package GUILayer;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import ModelLayer.DataStorage;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import java.awt.CardLayout;
import javax.swing.JLabel;

public class MainMenuUI {

	private JFrame frame;
	private JPanel mainMenuPanel;
	private JPanel mainContainer;
	private JPanel personsPanel;
	private JPanel leasesPanel;
	private JPanel productsPanel;
	private JPanel leaseItemsPanel;
	private JPanel groupPanel;
	private JPanel salePanel;
	private LoginUI loginUI;
	private JPanel statisticPanel;
	private JPanel mainProgramPanel;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainMenuUI window = new MainMenuUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		DataStorage dataStorage = null;
		try {
			dataStorage = new DataStorage();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			try {
				dataStorage.loadAll();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public MainMenuUI() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame("Inventory System");

		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				close();
			}
		});

		UIManager.put("Button.select", Color.decode("#C3C3C3"));
		UIManager.put("Menu.selectionBackground", Color.decode("#C3C3C3"));
		UIManager.put("MenuItem.selectionBackground", Color.decode("#C3C3C3"));
		UIManager.put("TabbedPane.selected", Color.decode("#FFFFFF"));
		UIManager.put("TabbedPane.focus", Color.decode("#FFFFFF"));
		UIManager.put("TabbedPane.selectHighlight", Color.decode("#FFFFFF"));
		UIManager.put("TabbedPane.borderHightlightColor",
				Color.decode("#C3C3C3"));

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		frame.setSize(1000, 700);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.getContentPane().setLayout(new CardLayout(0, 0));

		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmSave = new JMenuItem("Save");
		mnFile.add(mntmSave);

		JMenuItem mntmClose = new JMenuItem("Close");
		mntmClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		mnFile.add(mntmClose);

		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);

		JMenuItem mntmAbout = new JMenuItem("About");
		mnHelp.add(mntmAbout);

		loginUI = new LoginUI();

		frame.getContentPane().add(loginUI, "loginUI");

		loginUI.getLoginBtnLogin().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginUI.login();
				if (LoginUI.getLoggedInId() != 0) {
					loginInit();
				}
			}
		});
	}

	public void loginInit() {

		mainProgramPanel = new JPanel();
		frame.getContentPane().add(mainProgramPanel, "MainProgramPanel");
		mainProgramPanel.setLayout(new BorderLayout(0, 0));

		mainMenuPanel = new JPanel();
		mainProgramPanel.add(mainMenuPanel, BorderLayout.NORTH);
		mainMenuPanel.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		mainMenuPanel.add(panel, BorderLayout.WEST);

		JButton salesMainButton = new MainMenuButton("Sales");
		panel.add(salesMainButton);

		JButton leasesMainButton = new MainMenuButton("Leases");
		panel.add(leasesMainButton);

		JButton personsMainButton = new MainMenuButton("Persons");
		panel.add(personsMainButton);

		JButton productsMainButton = new MainMenuButton("Products");
		panel.add(productsMainButton);

		JButton leaseItemsMainButton = new MainMenuButton("Lease Items");
		panel.add(leaseItemsMainButton);

		JButton groupsMainButton = new MainMenuButton("Groups");
		panel.add(groupsMainButton);

		JPanel panel_1 = new JPanel();
		mainMenuPanel.add(panel_1, BorderLayout.EAST);

		String loggedInLbl = "You are logged in as "
				+ LoginUI.getLoggedInName();

		JLabel mainMenuLoggedIn = new JLabel(loggedInLbl);
		panel_1.add(mainMenuLoggedIn);

		MainMenuButton mainMenuButton = new MainMenuButton("Log out");
		panel_1.add(mainMenuButton);

		mainContainer = new JPanel();
		mainContainer.setLayout(new CardLayout(0, 0));

		mainProgramPanel.add(mainContainer);

		personsPanel = new PersonUI();
		mainContainer.add(personsPanel, "personsPanel");

		leasesPanel = new LeaseUI();
		mainContainer.add(leasesPanel, "leasesPanel");

		productsPanel = new SaleItemUI();
		mainContainer.add(productsPanel, "productsPanel");

		leaseItemsPanel = new LeaseItemUI();
		mainContainer.add(leaseItemsPanel, "leaseItemsPanel");

		groupPanel = new GroupUI();
		mainContainer.add(groupPanel, "groupPanel");

		salePanel = new SaleUI();
		mainContainer.add(salePanel, "salesPanel");

		statisticPanel = new StatisticUI();
		mainContainer.add(statisticPanel, "statisticPanel");

		JButton statisticsMainButton = new MainMenuButton("Statistics");
		panel.add(statisticsMainButton);

		statisticsMainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "statisticPanel");
			}
		});
		groupsMainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "groupPanel");
			}
		});
		leaseItemsMainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				((LeaseItemUI) leaseItemsPanel).showAllLeaseItems();
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "leaseItemsPanel");
			}
		});
		productsMainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				((SaleItemUI) productsPanel).showAllSaleItems();
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "productsPanel");
			}
		});
		personsMainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "personsPanel");
			}
		});
		leasesMainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "leasesPanel");
			}
		});
		salesMainButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "salesPanel");
			}
		});

		mainMenuButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.getContentPane().remove(mainProgramPanel);
				LoginUI.logout();
				CardLayout cl = (CardLayout) frame.getContentPane().getLayout();
				cl.show(frame.getContentPane(), "loginUI");
			}
		});

		salesMainButton.doClick();

		CardLayout cl = (CardLayout) frame.getContentPane().getLayout();
		cl.show(frame.getContentPane(), "MainProgramPanel");
	}

	public void close() {
		try {
			DataStorage dataStorage = new DataStorage();
			dataStorage.saveAll();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.exit(1);
	}
}
